<?php

declare(strict_types=1);

namespace app\admin\service\user;

use app\api\lib\SuPengJun;
use app\common\model\circle\CircleFansModel;
use app\common\model\dynamic\DynamicAudioModel;
use app\common\model\dynamic\DynamicCommentModel;
use app\common\model\dynamic\DynamicImgModel;
use app\common\model\dynamic\DynamicLikeModel;
use app\common\model\dynamic\DynamicModel;
use app\common\model\dynamic\DynamicVideoModel;
use app\common\model\goods\GoodsModel;
use app\common\model\goods\GoodsProductModel;
use app\common\model\NoticeModel;
use app\common\model\order\OrderGoodsModel;
use app\common\model\order\OrderModel;
use app\common\model\order\OrderRefundModel;
use app\common\model\user\UserAddressModel;
use app\common\model\user\UserCartModel;
use app\common\model\user\UserFollowModel;
use app\common\model\user\UserModel;
use app\common\model\user\UserPrivacyModel;
use app\Request;
use Dengje\Pxy\BaseService;

class UserService extends BaseService
{
    protected $model = null;


    public function __construct()
    {
        $this->model =  new \app\common\model\user\UserModel();
    }
    public function lists()
    {
        $list = $this->model->field('id,name')->order('id', 'desc')->select();
        return $list;
    }
    public function list()
    {
        $where = [];
        $id = request()->get('id');
        $name = request()->get('name');
        $mobile = request()->get('mobile');
        $status = request()->get('status');
        if ($status != 99) {
            $where[] = ['status', '=', $status];
            $where[] = ['type', '=', 1];
        } else {
            $where[] = ['type', '=', 2];
        }
        if ($id) $where[] = ['id', '=', $id];
        if ($name) $where[] = ['name', 'like', '%' . $name . '%'];
        if ($mobile) $where[] = ['mobile', '=', $mobile];
        $list = $this->model->where($where)->order('id', 'desc')->paginate(request()->get('page_size/d', 10));
        $list->map(function ($v) {
            $province = $v['province'];
            if (!$province) $province = $v['city'];
            if (!$province) $province = $v['country'];
            $v['ip_str'] = SuPengJun::addsStr($province);
            return $v;
        });
        return $list;
    }

    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->find();
//        $user = UserModel::where('id', $detail['user_id'])->field('type,name')->find();
//        halt($detail);
        // if ($user['type'] != 2) $detail['user']  = $user;
        return $detail;
    }

    public function create($data)
    {
        $data = $this->preExcludeFields($data);
        $province = $data['province'];
        $data = SuPengJun::ipStr($data, request()->ip());
        if ($province) $data['province'] = $province;
        unset($data['user']);
        $uid = $this->model->insertGetId($data);
        (new UserPrivacyModel())->insert(['user_id' => $uid, 'create_time' => time()]);
        return $uid;
    }

    public function update($id, $data)
    {
        $data = $this->preExcludeFields($data);
        unset($data['user']);
        return $this->model->where('id', $id)->update($data);
    }

    public function delete($id)
    {
        $this->model->where('id', $id)->delete();
        UserPrivacyModel::where('user_id', $id)->delete();
        NoticeModel::where('user_id', $id)->delete();
        NoticeModel::where('launch_id', $id)->delete();
        UserCartModel::where('user_id', $id)->delete();
        UserFollowModel::where('user_id', $id)->delete();
        UserFollowModel::where('follow_id', $id)->delete();
        UserAddressModel::where('user_id', $id)->delete();
        CircleFansModel::where('user_id', $id)->delete();
        $uoids = OrderModel::where('user_id', $id)->column('id');
        OrderGoodsModel::whereIn('order_id', $uoids)->delete();
        OrderRefundModel::whereIn('order_id', $uoids)->delete();
        $udids = DynamicModel::where('user_id', $id)->column('id');
        DynamicLikeModel::where('user_id', $id)->delete();
        DynamicLikeModel::whereIn('dynamic_id', $udids)->delete();
        DynamicImgModel::whereIn('dynamic_id', $udids)->delete();
        DynamicAudioModel::whereIn('dynamic_id', $udids)->delete();
        DynamicVideoModel::whereIn('dynamic_id', $udids)->delete();
        $dcids = DynamicCommentModel::whereIn('dynamic_id', $udids)->column('id');
        DynamicCommentModel::whereIn('reply_comment_id', $dcids)->delete();
        DynamicCommentModel::whereIn('dynamic_id', $udids)->delete();
        $udcids = DynamicCommentModel::where('user_id', $id)->column('id');
        DynamicCommentModel::whereIn('reply_comment_id', $udcids)->delete();
        DynamicCommentModel::where('user_id', $id)->delete();
        return success('删除成功！');
    }

    public function cart()
    {
        $where[] = ['status', '<>', 0];
        $userId = request()->get('user_id');
        if ($userId) $where[] = ['user_id', '=', $userId];
        $list = UserCartModel::where($where)->paginate(request()->get('page_size/d', 10));
        $list->map(function ($v) {
            $v['user'] = UserModel::where('id', $v['user_id'])->field('name,avatar')->find();
            $v['goods_name'] = GoodsModel::where('id', $v['goods_id'])->value('name');
            $v['product'] = GoodsProductModel::where('id', $v['goods_product_id'])
                ->field('price,line_price,img,name')->find();
            return $v;
        });
        return $list;
    }

    public function del_cart($id)
    {
        UserCartModel::where('id', $id)->delete();
        return success('删除成功！');
    }

    public function privacy()
    {
        $where[] = ['examine', '=', 1];
        $userId = request()->get('user_id');
        if ($userId) $where[] = ['user_id', '=', $userId];
        $list = UserPrivacyModel::where($where)->paginate(request()->get('page_size/d', 10));
        $list->map(function ($v) {
            $v['user'] = UserModel::where('id', $v['user_id'])->field('name,avatar,intro')->find();
            return $v;
        });
        return $list;
    }

    public function examine($id, $data)
    {
        $data = $this->preExcludeFields($data);
        $model = ['examine' => 0];
        $avatarUrl = '/pages/center/means?sx=1';
        if ($data['type'] == 1) {
            $userModel = ['update_time' => time()];
            if ($data['name']) $userModel['name'] = $data['name'];
            if ($data['avatar']) $userModel['avatar'] = $data['avatar'];
            if ($data['intro']) $userModel['intro'] = $data['intro'];
            UserModel::where('id', $data['user_id'])->update($userModel);
            $content = '审核结果：通过，账号信息若未自动更新，您可以在个人主页下拉刷新。';
        } else {
            $content = '审核结果：驳回，您可以重新提交更新，请勿提交违规信息。';
        }
        $model['name'] = '';
        $model['avatar'] = '';
        $model['intro'] = '';
        (new NoticeModel())->send($data['user_id'], '个人资料通知', $content, 0, $avatarUrl, $avatarUrl);
        return UserPrivacyModel::where('id', $id)->update($model);
    }

    public function select()
    {
        $where[] = ['status', '=', 1];
        $name = request()->get('name');
        if ($name) $where[] = ['name', 'like', '%' . $name . '%'];
        $list = $this->model->where($where)->field('id,name')->select();
        return $list;
    }
}
